package at.ac.tuwien.infosys.aic11.server;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.jaxws.EndpointImpl;

import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.apache.cxf.ws.security.wss4j.WSS4JInInterceptor;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.handler.WSHandlerConstants;

import at.ac.tuwien.infosys.aic11.services.ContractManagementImpl;
import at.ac.tuwien.infosys.aic11.services.RatingService;
import at.ac.tuwien.infosys.aic11.services.RatingServiceImpl;
import at.ac.tuwien.infosys.aic11.services.ShippingServiceImpl;


public class Server {
	
	private static Logger log = Logger.getLogger("Server");

	protected Server() throws Exception {
		JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
		sf.setResourceClasses(RatingService.class);
		sf.setResourceProvider(RatingService.class,
				new SingletonResourceProvider(new RatingServiceImpl() {
				}));
		sf.setAddress("http://localhost:9000/");
		sf.create();

		ContractManagementImpl implementor = new ContractManagementImpl();
		String address = "http://localhost:9001/ContractManagementService";
		EndpointImpl jaxWsEndpoint = (EndpointImpl) javax.xml.ws.Endpoint
				.publish(address, implementor);
		Endpoint cxfEndpoint = jaxWsEndpoint.getServer().getEndpoint();

		Map<String, Object> inProps = new HashMap<String, Object>();
		inProps.put(WSHandlerConstants.ACTION, 
			    WSHandlerConstants.TIMESTAMP + " " + 
			    WSHandlerConstants.SIGNATURE + " " + 
			    WSHandlerConstants.ENCRYPT);
		inProps.put(WSHandlerConstants.SIG_PROP_FILE, "META-INF/crmserver_cert.properties");
		inProps.put(WSHandlerConstants.DEC_PROP_FILE,
				"META-INF/crmserver_cert.properties");
		inProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				at.ac.tuwien.infosys.aic11.server.PasswordHandler.class.getName());

		WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
		cxfEndpoint.getInInterceptors().add(wssIn);

		Map<String, Object> outProps = new HashMap<String, Object>();
		outProps.put(WSHandlerConstants.ACTION, 
			    WSHandlerConstants.TIMESTAMP + " " + 
			    WSHandlerConstants.SIGNATURE + " " + 
			    WSHandlerConstants.ENCRYPT);
		outProps.put(WSHandlerConstants.USER, "crmserver");
		outProps.put(WSHandlerConstants.ENCRYPTION_USER, "clerk");
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				at.ac.tuwien.infosys.aic11.server.PasswordHandler.class.getName());
		outProps.put(WSHandlerConstants.SIG_PROP_FILE, "META-INF/crmserver_cert.properties");
		outProps.put(WSHandlerConstants.ENC_PROP_FILE,
				"META-INF/crmserver_cert.properties");

		WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
		
		cxfEndpoint.getOutInterceptors().add(wssOut);
		
		log.info("wss4j setup done");

		
		ShippingServiceImpl ssi = new ShippingServiceImpl();
		String ssiAddress = "http://localhost:9001/ShippingService";
		javax.xml.ws.Endpoint.publish(ssiAddress, ssi);
	}

	public static void main(String args[]) throws Exception {
		new Server();
		log.info("Server ready...");

		Thread.sleep(600 * 60 * 1000);
		log.info("Server exiting");
	}
}